/*
 * Copyright the original author or authors.
 * 
 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.mozilla.org/MPL/MPL-1.1.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.pixlib.sfe
{
	import net.pixlib.core.PixlibApplication;
	import net.pixlib.core.pixlib_internal;
	import net.pixlib.events.ObjectEvent;
	import net.pixlib.log.Log;
	import net.pixlib.log.Logger;
	import net.pixlib.log.layout.AirLoggerLayout;
	import net.pixlib.sfe.command.app.Init;
	import net.pixlib.sfe.event.EventList;
	import net.pixlib.sfe.log.Debug;
	import net.pixlib.sfe.plugin.AbstractSpritePlugin;
	import net.pixlib.sfe.util.nsCONFIG;

	import flash.desktop.NativeApplication;
	import flash.events.Event;
	import flash.events.InvokeEvent;
	import flash.filesystem.File;

	/**
	 * PixFontEditor class is the main application class.
	 * 
	 * @author Romain Ecarnot
	 */
	public class PixFontEditor extends AbstractSpritePlugin 
	{
		//--------------------------------------------------------------------
		// Constants
		//--------------------------------------------------------------------
		
		/** Extension for application files. */
		public static const APP_EXTENSION : String = ".pxsfl";
		
		
		//--------------------------------------------------------------------
		// Public API
		//--------------------------------------------------------------------
		
		/**
		 * Application constructor.
		 */
		public function PixFontEditor()
		{
			super(getConstructorAccess());
		}
		
		/**
		 * Returns application's debugging channel.
		 */
		override public function getLogger() : Log
		{
			return Debug.getInstance();
		} 

		//--------------------------------------------------------------------
		// Protected methods
		//--------------------------------------------------------------------
				
		/**
		 * Initializes plugin engine.
		 */
		override protected function initialize( ) : void
		{
			super.initialize();
			
			_initLogger();
			_initStage();
			_initController();
			
			firePrivateEvent(new Event(EventList.INIT));
		}

		
		//--------------------------------------------------------------------
		// Private methods
		//--------------------------------------------------------------------
		
		/**
		 * Inits application log listeners.
		 */
		private function _initLogger() : void
		{
			if(nsCONFIG::productionMode == false) 
			{
    			Logger.getInstance().addLogListener(AirLoggerLayout.getInstance());
			}
		}		
		
		/**
		 * Inits application stage properties.
		 */
		private function _initStage() : void
		{
			PixlibApplication.getInstance().pixlib_internal::init(this, loaderInfo);
		}
		
		/**
		 * Inits Application Front Controller and events listeners.
		 */
		private function _initController() : void
		{
			NativeApplication.nativeApplication.addEventListener(InvokeEvent.INVOKE, _onInvokeHandler);
			 
			NativeApplication.nativeApplication.addEventListener(Event.EXITING, _onExitingHandler);
			stage.nativeWindow.addEventListener(Event.CLOSING, _onExitingHandler);
			
			getController().pushCommandClass(EventList.INIT, Init);
		}

		/**
		 * Triggered when application is running from command line or 
		 * when a PixFontEditor project file is opened from system.
		 */
		private function _onInvokeHandler(event : InvokeEvent) : void 
		{ 
			stage.nativeWindow.visible = true;
			
			if(event.arguments.length > 0)
			{
				var dir : File = event.currentDirectory; 
				var file : File = dir.resolvePath(event.arguments[0]);
				
				firePrivateEvent(new ObjectEvent(EventList.LOAD_PROJECT, this, file));
			}
		} 
		
		/**
		 * Triggered when application is existing.
		 */
		private function _onExitingHandler(event : Event) : void 
		{ 
			event.preventDefault();
			firePrivateEvent(new Event(EventList.EXIT));
		} 
	}
}
